package com.jiankangjin.weixinchannel.services.impl;

import java.util.HashMap;
import java.util.Map;

import com.jiankangjin.weixinchannel.config.WeixinConfig;
import com.jiankangjin.weixinchannel.util.MD5Util;
import com.jiankangjin.weixinchannel.util.Sha1Util;
import com.jiankangjin.weixinchannel.util.TenpayHttpClient;
import com.jiankangjin.weixinchannel.util.XMLUtil;
import com.jiankangjin.weixinchannel.vo.QueryVo;

/**
 * 向微信查订单状态
 */
public class QueryServiceImpl {
//	private static final transient Log logger = LogFactory.getLog(QueryServiceImpl.class);
	
	public QueryVo barcode_V0_0_1(
			String pay_ssn_id,
			String order_id, 
			String pay_chnl_seq, 
			String expand_json) {
		QueryVo queryVo = new QueryVo();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("appid", WeixinConfig.APP_ID);
			params.put("mch_id", WeixinConfig.MCH_ID);
//			params.put("sub_mch_id", "");// 子商户号
			params.put("transaction_id", pay_chnl_seq);	// 微信订单号
			params.put("out_trade_no", pay_ssn_id);		// 商户订单号
			params.put("nonce_str", Sha1Util.getNonceStr());// 随机串
			Map<String, Object> sPara = MD5Util.MD5Encode(params, "utf8");
			String xmlStr = XMLUtil.toXMLstr(sPara);
//	    	logger.info("调微信通用订单查询接口:"+xmlStr);
			TenpayHttpClient httpClient = new TenpayHttpClient();
			String url = "https://api.mch.weixin.qq.com/pay/orderquery";
			Map<String, String> weixinMap = null;
	    	if(httpClient.callHttpPost(url, xmlStr)) {
		    	weixinMap = XMLUtil.doXMLParse(new String(httpClient.getResContent().getBytes("gbk"), "utf8"));
//				logger.info("微信通用订单查询返回:"+weixinMap);
	    	}
	    	// SUCCESS--支付成功 REFUND--转入退款 NOTPAY--未支付 CLOSED--已关闭 REVOKED--已撤销
	    	// USERPAYING--用户支付中 NOPAY--未支付(确认支付超时) PAYERROR--支付失败
	    	String trade_state = weixinMap.get("trade_state");
	    	if ("SUCCESS".equals(trade_state) || "SUCCESS".equals(trade_state)) {
	    		queryVo.setSub_deal_status("1001");
	    		queryVo.setSub_deal_msg("成功支付");
	    		queryVo.setPay_amt(weixinMap.get("total_fee"));
	    	} else if ("USERPAYING".equals(trade_state)) {
	    		queryVo.setSub_deal_status("0021");
	    		queryVo.setSub_deal_msg("用户支付中");
			} else if ("NOTPAY".equals(trade_state) || "PAYERROR".equals(trade_state)) {
	    		queryVo.setSub_deal_status("0041");
	    		queryVo.setSub_deal_msg("支付失败");
			} else if ("CLOSED".equals(trade_state) || "REVOKED".equals(trade_state)) {
	    		queryVo.setSub_deal_status("0071");
	    		queryVo.setSub_deal_msg("交易关闭");
			}
	    	queryVo.setPay_fin_time(weixinMap.get("time_end"));
	    	queryVo.setPay_chnl_mer(weixinMap.get("mch_id"));
	    	queryVo.setPay_user("");
	    	queryVo.setPay_ssn_id(pay_ssn_id);
	    	queryVo.setPay_chnl_seq(pay_chnl_seq);
	    	queryVo.setPay_chnl_pos("");
	    	queryVo.setPay_chnl_fee("0");
	    	queryVo.setResp_status("0000");
	    	queryVo.setResp_msg("成功");
	    	queryVo.setSign("AlipayChannel_Query_sign");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return queryVo;
	}
	public QueryVo public_V2(
			String pay_ssn_id,
			String order_id, 
			String pay_chnl_seq, 
			String expand_json) {
		return null;
	}
	public QueryVo public_V3(
			String pay_ssn_id,
			String order_id, 
			String pay_chnl_seq, 
			String expand_json) {
		return null;
	}
}
/*
 *trade_state=REFUND, 
 *return_msg=OK, 
 *is_subscribe=Y, 
 *appid=wxcec8863ae602c67c, 
 *fee_type=CNY, 
 *out_trade_no=20140924145114, 
 *nonce_str=vgNPjEctiXuUNg9l, 
 *device_info=1000, 
 *transaction_id=1003810706201409240005174313, 
 *trade_type=MICROPAY, 
 *sign=301793CADBECA1A0B0848009FDE1E6EF, 
 *result_code=SUCCESS, 
 *mch_id=10017474, 
 *attach=yjwkhhbj_secondsun_hello, 
 *total_fee=1, sub_mch_id=, 
 *time_end=20140924145119, 
 *openid=obDekuBWSsFOzgcXqwRamlEA26BI, 
 *bank_type=CCB_CREDIT, 
 *return_code=SUCCESS 
 */
